/*
 * @Author: yc
 * @Date: 2020-05-29 10:39:50
 * @LastEditTime: 2020-05-31 11:30:19
 * @FilePath: \react-admin\src\redux\reducers\notifications.js
 */
import { actionTypes } from '../actions/notifications'

const initState = {
  isLoading: false,
  list: []
}

const notificationsReducer = (state = initState, action) => {
  switch(action.type) {
    case actionTypes.START_NOTIFICATION_POST:
      return {
        ...state,
        isLoading: true
      }
    case actionTypes.FINISH_NOTIFICATION_POST:
      return {
        ...state,
        isLoading: false
      }
    case actionTypes.MARK_NOTIFICATION_AS_READ_BY_ID:
      const newList = state.list.map(item => {
        if(item._id === action.payload.id) {
          item.hasRead = true
        }
        return item
      })
      return {
        ...state,
        list: newList
      }
    case actionTypes.MARK_ALL_NOTIFICATIONS_AS_READ:
      return{
        ...state,
        list: state.list.map(item => {
          item.hasRead = true
          return item
        })
      }
    case actionTypes.RECIVED_NOTIFICATIONS:
      return {
        ...state,
        list: action.payload.list
      }
    default:
      return state
  }
}

export default notificationsReducer
